//
//  Student.m
//  CompareDemo
//
//  Created by 向祖华 on 16/4/27.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "Student.h"

@implementation Student

//初始化方法
-(id)initWithName:(NSString*)name
              age:(NSInteger)age
            score:(CGFloat)score
           number:(NSInteger)number{
    self = [super init];
    if (self != nil) {
        self.name = name;
        self.age = age;
        self.score = score;
        self.number = number;
    }
    return self;
}
//遍历构造器
+(id)studentWithName:(NSString*)name
                 age:(NSInteger)age
               score:(CGFloat)score
              number:(NSInteger)number{
    Student * student = [[Student alloc] initWithName:name age:age score:score number:number];
    return student;
}
//重写description的方法
-(NSString *)description{
    return [NSString stringWithFormat:@"name:%@,age:%ld,socre:%.1f,number:%ld",
            self.name, self.age, self.score, self.number];
}
//按照学生姓名降序排序
-(NSComparisonResult)compareByName:(Student*)student{
    return -[self.name compare:student.name];//加“-”标识降序，否则为升序
}
//按照学生年龄排序
-(NSComparisonResult)compareByAge:(Student*)student{
//    return [@(self.age) compare:@(student.age)];
    //或者下面方法
    return self.age > student.age ? NSOrderedDescending: self.age == student.age ? NSOrderedSame : NSOrderedAscending;
}
//按照学生分数降序排序
-(NSComparisonResult)compareByScore:(Student*)student{
    return -[@(self.score) compare: @(student.score)];
    //return self.score < stu.score ? NSOrderedDescending :self.score == stu.score ? NSOrderedSame : NSOrderedAscending;
}
//按照学生学号排序
-(NSComparisonResult)compareByNumber:(Student*)student{
    return [@(self.number) compare: @(student.number)];
    //return self.number > stu.number ? NSOrderedDescending: self.number == stu.number ? NSOrderedSame : NSOrderedAscending;
}


@end
